/* 
** InitTextures.cpp
**
** Initialize an OpenGL texture using SOIL.
*/

#include <math.h>
#include <assert.h>

#include <stdlib.h>
#include <unistd.h>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include "InitTextures.h"

#include <SOIL.h>

#include <stdio.h>

/*
** InitTextures(name)
**
** Create an OpenGL texture object initialized with the contents
** of a supplied texture image file.
**
** Arguments:
**      filename        pathname to the file containing the texture image
**
** Returns:
**      On success, the texture ID
**      Else, prints an error message and exits
*/

GLuint InitTextures( const char *filename ) {
	GLuint id;

	// Use the SOIL library to load this texture image
	id = SOIL_load_OGL_texture( filename,
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y |
			SOIL_FLAG_NTSC_SAFE_RGB |
			SOIL_FLAG_COMPRESS_TO_DXT );
	
	// Verify that the load succeeded
	if( id == 0 ) {
		printf( "SOIL error '%s'\n",
			SOIL_last_result() );
		exit( 1 );
	}
	 
	// Success - configure the texture environment
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

	// Return the texture ID
	return id;

}
